<?php
class Nadeb_Form_Checkbox
{
	private $label;
	private $id;
	private $options;
	private $value;
	private $param;
	
	private $inputTag;
	private $tag = "";
	
	
	public function __construct($_param)
	{
		$this->label   = $_param["label"];
		$this->id      = $_param["id"];
		$this->options = $_param["options"];
		$this->param   = $_param;
		
		unset($this->param["rules"]);
		unset($this->param["options"]);
		unset($this->param["label"]);
		unset($this->param["id"]);
		
		if( isset( $_param["value"] ) )
		{
			$this->value = $_param["value"];
			unset($this->param["value"]);
		}
	}
	
	public function create_object()
	{
		/*
		 * input TAG
		 */
		$this->value = str_replace(", ",",",$this->value);
		$this->value = str_replace(" ,",",",$this->value);
		
		$ar_value = explode(",",trim($this->value));
		
		if(count($this->options) == 1)
		{
			if( $this->options[0] == $this->value )
				$this->inputTag .= "\n\t\t\t<label><input name=\"{$this->param['name']}[]\" type=\"checkbox\" value=\"{$this->options[0]}\" checked=\"checked\" /> <span>{$this->label}</span></label>";
			else
				$this->inputTag .= "\n\t\t\t<label><input name=\"{$this->param['name']}[]\" type=\"checkbox\" value=\"{$this->options[0]}\" /> <span>{$this->label}</span></label>";
		}
		else
		{
			
			foreach($this->options as $key => $value)
			{
				if( in_array($value,$ar_value) || in_array($key,$ar_value) )
					$this->inputTag .= "\n\t\t\t<label><input name=\"{$this->param['name']}[]\" type=\"checkbox\" value=\"{$key}\" checked=\"checked\" /> <span>{$value}</span></label>";
				else
					$this->inputTag .= "\n\t\t\t<label><input name=\"{$this->param['name']}[]\" type=\"checkbox\" value=\"{$key}\" /> <span>{$value}</span></label>";
			}
		}
		
		/*
		 * dt TAG
		 */
		if(count($this->options) > 1)
			$this->tag .= "
		<dt id=\"{$this->id }-label\">
			<label class=\"{$this->param["name"]}-label\">{$this->label}</label>
		</dt>";

		
		/*
		 * dd TAG
		 */
		$this->tag .= "
		<dd id=\"{$this->id }-object\">$this->inputTag
		</dd>";
		
		
	}
	
	public function get_xhtml()
	{
		return $this->tag;
	}
}

